/* eslint-disable @typescript-eslint/no-unused-vars */
import gm from 'gray-matter'
import useIcon from '../../hooks/useIcon';
import type { BasicResult, BiomesResult, BlocksResult, EffectsResult, EnchantmentsResult, EntitiesResult, LiverResult, SingleItemResult } from '@/types/search';
import ColorMap from '../../styles/PersonalThemeColor';
import pinyin from 'pinyin';
import getCnTypeFromEnType from '../../utils/getCnTypeFromEnType';

const cacheLib = new Map();

function getPinyin4Id(id: string, name: string): string[] {
  if (cacheLib.has(id))
    return cacheLib.get(id)
  const res = pinyin(name, { style: pinyin.STYLE_NORMAL, group: false }).map((item) => item[0])
  cacheLib.set(id, res)
  return res
}

export function BasicResultGenerator(path: string, content: string, classify: string): BasicResult {
  const id = path.split("/").pop()!;
  const { data: { name: title } } = gm(content)
  const p = getPinyin4Id(id, title)

  return {
    id,
    type: "",
    title,
    pinyin: p.join(""),
    pinyinHead: p.map((item) => item[0]).join(""),
    link: `/wiki/${classify}/${id}`,
  }
}

export function LiverResultGenerator(name: keyof typeof ColorMap) {
  return {
    id: name,
    type: "Liver",
    title: ColorMap[name].cnName,
    subTitle: `物品一览 - ${ColorMap[name].cnName}`,
    link: `/wiki/preview#${name}`,
  } as LiverResult
}

export function ItemResultGenerator(path: string, content: string) {
  // md.render(content)
  // const { data } = gm(content)
  const res: SingleItemResult = {
    id: "",
    type: "物品",
    title: "",
    subTitle: "",
    link: "",
  }
  const splitList = path.split("/");
  if (splitList.length === 3) {
    const [_, type, id] = splitList
    const icon = useIcon(id)
    if (!icon) {
      const { data: { name } } = gm(content)
      res.title = name
    }
    else
      res.title = icon.name

    res.id = id
    res.link = `/wiki/items/${id}`
    if (type === "Other") {
      res.subTitle = "其他物品"
    }
    else {
      res.subTitle = "赞助者物品"
    }
    const p = getPinyin4Id(id, res.title)
    res.pinyin = p.join("")
    res.pinyinHead = p.map((item) => item[0]).join("")
  }
  else {
    const [_1, _2, member, id] = splitList
    const icon = useIcon(id), color = ColorMap[member.toLowerCase() as keyof typeof ColorMap];
    if (!icon) {
      const { data: { name } } = gm(content)
      res.title = name
    }
    else
      res.title = icon.name
    res.id = id
    res.link = `/wiki/items/${id}`
    res.subTitle = color.cnName

    const p = getPinyin4Id(id, res.title)
    res.pinyin = p.join("")
    res.pinyinHead = p.map((item) => item[0]).join("")
  }

  return res
}

export function EnchantmentsResultGenerator(path: string, content: string) {
  return {
    ...BasicResultGenerator(path, content, "enchantments"),
    type: getCnTypeFromEnType("enchantments"),
  } as EnchantmentsResult
}

export function BiomesResultGenerator(path: string, content: string) {
  return {
    ...BasicResultGenerator(path, content, "biomes"),
    type: getCnTypeFromEnType("biomes"),
  } as BiomesResult
}

export function BlocksResultGenerator(path: string, content: string) {
  const res = BasicResultGenerator(path, content, "blocks")
  const [type, id] = path.split("/")

  const icon = useIcon(id)
  if (icon)
    res.title = icon.name

  return {
    ...res,
    type: getCnTypeFromEnType("blocks"),
  } as BlocksResult
}

export function EffectsResultGenerator(path: string, content: string) {
  return {
    ...BasicResultGenerator(path, content, "effects"),
    type: getCnTypeFromEnType("effects"),
  } as EffectsResult
}

export function EntitiesResultGenerator(path: string, content: string) {
  return {
    ...BasicResultGenerator(path, content, "entities"),
    type: getCnTypeFromEnType("entities"),
  } as EntitiesResult
}

export default function ResultGenerator(path: string, content: string) {
  if (path.includes("items")) return ItemResultGenerator(path, content)
  else if (path.includes("effects")) return EffectsResultGenerator(path, content)
  else if (path.includes("enchantments")) return EnchantmentsResultGenerator(path, content)
  else if (path.includes("biomes")) return BiomesResultGenerator(path, content)
  else if (path.includes("blocks")) return BlocksResultGenerator(path, content)
  else if (path.includes("entities")) return EntitiesResultGenerator(path, content)
}
